package com.fenqing.leetcode.code.t0_200.t100_200;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fenqing
 */
public class Topic118 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic118(String input) {
        super(input);
    }

    @Execute
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            List<Integer> last = null;
            if (i > 0) {
                last = result.get(i - 1);
            }
            List<Integer> row = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                if (last == null) {
                    row.add(1);
                } else {
                    int num = 0;
                    if (last.size() > j) {
                        num += last.get(j);
                    }
                    if (last.size() > j - 1 && j - 1 >= 0) {
                        num += last.get(j - 1);
                    }
                    row.add(num == 0 ? 1 : num);
                }
            }
            result.add(row);
        }
        return result;
    }

    public static void main(String[] args) {
        List<List<Integer>> run = new Topic118("11").run();
        run.forEach(System.out::println);
    }
}

/**
 * 1 = 0
 * 2 = 0
 * 3 = 2
 * 4 =
 * 5 = 6
 * 6 =
 * 7 = 20
 * 9 = 70
 * 11 = 252
 */